package com.tungse.juc.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class Future_Demo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //无返回值的 异步执行
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("runAsync");
//        });
//        System.out.println("11111111111");
//        completableFuture.get();

        //有返回值的 异步执行
        CompletableFuture<String> completableFutureR = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + " supplyAsync => String");
            int i = 1/0;
            return "1111111111";
        });
        System.out.println(completableFutureR.whenComplete((t, u) -> {
            System.out.println("t=> " + t); //正确执行的结果
            System.out.println("u=> " + u); //失败时的异常信息
        }).exceptionally((e) -> {
            System.out.println("exceptionally=> " + e.getMessage());
            return "2222222222";
        }).get());

    }
}
